package com.cyz.web.app.concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class ArrayListMultiThread {

    final static AddThread instance = new AddThread();


    //线程安全的,结果正确
    private volatile static List<Integer> al = new Vector<>(2 << 3);
    //线程不安全,结果不正确
//    private  volatile static List<Integer> al = new ArrayList<>(2<<3);

    public static class AddThread implements Runnable {

        @Override
        public void run() {
            for (int i = 0; i < 1000000; i++) {
                al.add(i);
            }
        }
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(instance);

        Thread t2 = new Thread(instance);

        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.printf("al的长度 %d \n", al.size());
    }

}
